#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: Comzyh
# @Date:   2014-10-26 18:35:19
# @Last Modified by:   Comzyh
# @Last Modified time: 2014-10-26 20:31:25
import os
def main():
	scores_dir = os.path.abspath(os.curdir)
	team_score = {}
	for filename in os.listdir(scores_dir):
		if filename.endswith(".log"):
			score_file = open(os.path.abspath(filename),'r')
			sum_score = 0.0
			score_copy = []
			for line in score_file.readlines():
				[team,score] = line.split("\t")
				score = float(score)
				sum_score += score
				if not team_score.has_key(team):
					team_score[team] = {
					'team':team,
					'score':0.0,
					'history':[]
					}
				score_copy.append((team,score))
			# print score_copy
			for team, score in score_copy:
				team_score[team]['score'] += 1.0 * score / sum_score
				team_score[team]['history'].append(1.0 * score / sum_score)
			score_file.close()
	result = team_score.values()
	result.sort(key = lambda x:x['score'],reverse = True)

	result_file = open('result.txt','w')
	for r in result:
		result_file.write("%s\t%s" % (r['team'],r['score']))
		for h in r['history']:
			result_file.write("\t%s" % h)
		result_file.write("\n")
	result_file.close()

if __name__ == '__main__':
	main()
